const state = {
  sidebar: {
    opened: localStorage.getItem('sidebarStatus') ? !!+localStorage.getItem('sidebarStatus') : true,
    withoutAnimation: false
  },
  device: 'desktop',
  loading: false,
  loadingText: '加载中...',
  breadcrumb: []
}

const mutations = {
  TOGGLE_SIDEBAR(state) {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      localStorage.setItem('sidebarStatus', 1)
    } else {
      localStorage.setItem('sidebarStatus', 0)
    }
  },
  
  CLOSE_SIDEBAR(state, withoutAnimation) {
    localStorage.setItem('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  
  TOGGLE_DEVICE(state, device) {
    state.device = device
  },
  
  SET_LOADING(state, loading) {
    state.loading = loading
  },

  SET_LOADING_TEXT(state, text) {
    state.loadingText = text
  },
  
  SET_BREADCRUMB(state, breadcrumb) {
    state.breadcrumb = breadcrumb
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  
  setLoading({ commit }, { loading, text = '加载中...' }) {
    commit('SET_LOADING', loading)
    commit('SET_LOADING_TEXT', text)
  },
  
  setBreadcrumb({ commit }, breadcrumb) {
    commit('SET_BREADCRUMB', breadcrumb)
  }
}

const getters = {
  sidebar: state => state.sidebar,
  device: state => state.device,
  loading: state => state.loading,
  loadingText: state => state.loadingText,
  breadcrumb: state => state.breadcrumb
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
